ploeh blog 2021-08-02T06:38:07+00:00 Mark Seemann danish software design https://blog.ploeh.dk 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</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>. 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> 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="/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>. 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></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</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></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</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></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</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></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> </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></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></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></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></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> <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></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></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></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></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></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></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></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></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></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></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></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></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></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></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></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's 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 you 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 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></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></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></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></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</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></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</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<IReservationsRepository>(); 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></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</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></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</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></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></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></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></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></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></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></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></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></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>)</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></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> </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></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></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> </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></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></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></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></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></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></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></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></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</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></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>N