Skip to content

Commit

Permalink
Prepare article for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
ploeh committed Sep 27, 2021
1 parent 414b059 commit e2539f4
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ <h3 id="a90284d241c4463583401504395a4a8a">
<ul>
<li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li>
<li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li>
<li>The Equivalence contravariant functor</li>
<li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li>
<li>Reader as a contravariant functor</li>
<li>Functor variance compared to C#'s notion of variance</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion _posts/2018-03-19-functors-applicatives-and-friends.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<ul>
<li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li>
<li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li>
<li>The Equivalence contravariant functor</li>
<li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li>
<li>Reader as a contravariant functor</li>
<li>Functor variance compared to C#'s notion of variance</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h3 id="b73fe8b865384190a031e05915227d9a">
<li><a href="/2021/05/10/simplifying-code-with-decorated-commands">Simplifying code with Decorated Commands</a></li>
<li><a href="/2021/09/13/unit-testing-private-helper-methods">Unit testing private helper methods</a></li>
<li><a href="/2021/09/20/keep-ids-internal-with-rest">Keep IDs internal with REST</a></li>
<li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li>
</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>
Expand Down
2 changes: 1 addition & 1 deletion _posts/2021-09-02-contravariant-functors.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ <h3 id="187c2160ddf94c119c341534189d3eab">
<ul>
<li><a href="/2021/09/06/the-command-handler-contravariant-functor">The Command Handler contravariant functor</a></li>
<li><a href="/2021/09/09/the-specification-contravariant-functor">The Specification contravariant functor</a></li>
<li>The Equivalence contravariant functor</li>
<li><a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a></li>
<li>Reader as a contravariant functor</li>
<li>Functor variance compared to C#'s notion of variance</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@ <h3 id="4c2e7471713547d5a24a20dacce49806">
Are you noticing a pattern?
</p>
<p>
<strong>Next:</strong> Reader as a contravariant functor.
<strong>Next:</strong> <a href="/2021/09/27/the-equivalence-contravariant-functor">The Equivalence contravariant functor</a>.
</p>
</div>
2 changes: 1 addition & 1 deletion _posts/2021-09-20-keep-ids-internal-with-rest.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ <h3 id="3cf78b876e2a4f91b3e49b91f89bde02">
This enables the <code>LinksFilter</code> and other internal code to still access the <code>Id</code> property, while the unit tests no longer could. As expected, this change caused some compiler errors. That was expected, and my plan was to <em>lean on the compiler</em>, as <a href="https://michaelfeathers.silvrback.com">Michael Feathers</a> describes in <a href="http://bit.ly/working-effectively-with-legacy-code">Working Effectively with Legacy Code</a>.
</p>
<p>
As I had hoped, relatively few things broke, and they were fixed in 5-10 minutes. Once everything compiled, I ran the tests. Only a single test failed, and this was a unit test that used some Back Door Manipulation, as <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> terms it. I'll return to that test in <a href="">a future article</a>.
As I had hoped, relatively few things broke, and they were fixed in 5-10 minutes. Once everything compiled, I ran the tests. Only a single test failed, and this was a unit test that used some Back Door Manipulation, as <a href="http://bit.ly/xunitpatterns">xUnit Test Patterns</a> terms it. I'll return to that test in <a href="/2021/09/27/the-equivalence-contravariant-functor">a future article</a>.
</p>
<p>
None of my self-hosted integration tests failed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: "The Equivalence contravariant functor"
description: "An introduction to the Equivalence contravariant functor for object-oriented programmers."
date: 2021-09-08 6:12 UTC
date: 2021-09-27 6:08 UTC
tags: [Software Design, Unit Testing]
---
{% include JB/setup %}
Expand All @@ -12,10 +12,10 @@
<em>{{ page.description }}</em>
</p>
<p>
This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. In previous articles, you saw examples of <a href="/2021/09/06/the-command-handler-contravariant-functor">the Command Handler</a> and <a href="">Specification</a> contravariant functors. This article presents another example.
This article is an instalment in <a href="/2021/09/02/contravariant-functors">an article series about contravariant functors</a>. It assumes that you've read the introduction. In previous articles, you saw examples of <a href="/2021/09/06/the-command-handler-contravariant-functor">the Command Handler</a> and <a href="/2021/09/09/the-specification-contravariant-functor">Specification</a> contravariant functors. This article presents another example.
</p>
<p>
In <a href="">a recent article</a> I described how I experimented with removing the <code>id</code> property from a JSON representation in a REST API. I also mentioned that doing that made one test fail. In this article you'll see the failing test and how the Equivalence contravariant functor can improve the situation.
In <a href="/2021/09/20/keep-ids-internal-with-rest">a recent article</a> I described how I experimented with removing the <code>id</code> property from a JSON representation in a REST API. I also mentioned that doing that made one test fail. In this article you'll see the failing test and how the Equivalence contravariant functor can improve the situation.
</p>
<h3 id="be01e1b437a94b2689f5fdd3a68ae33f">
Baseline <a href="#be01e1b437a94b2689f5fdd3a68ae33f" title="permalink">#</a>
Expand Down Expand Up @@ -79,7 +79,7 @@ <h3 id="be8ae690e33a4e7fb3b5d5f15d6ddce9">
Brittle assertion <a href="#be8ae690e33a4e7fb3b5d5f15d6ddce9" title="permalink">#</a>
</h3>
<p>
When I <a href="">made the <code>Id</code> property <code>internal</code></a>, this test no longer compiled. I had to delete the assignment of <code>Id</code>, which also meant that I couldn't use a deterministic <code>Guid</code> to define the <code>expected</code> value. While I could create an arbitrary <code>Guid</code>, that would never pass the test, since the <code>Post</code> method also generates a new <code>Guid</code>.
When I <a href="/2021/09/20/keep-ids-internal-with-rest">made the <code>Id</code> property <code>internal</code></a>, this test no longer compiled. I had to delete the assignment of <code>Id</code>, which also meant that I couldn't use a deterministic <code>Guid</code> to define the <code>expected</code> value. While I could create an arbitrary <code>Guid</code>, that would never pass the test, since the <code>Post</code> method also generates a new <code>Guid</code>.
</p>
<p>
In order to <a href="/2019/10/21/a-red-green-refactor-checklist">get to green</a> as quickly as possible, I rewrote the assertion:
Expand All @@ -99,7 +99,7 @@ <h3 id="be8ae690e33a4e7fb3b5d5f15d6ddce9">
It's brittle because it explicitly considers the four properties <code>At</code>, <code>Email</code>, <code>Name</code>, and <code>Quantity</code> of the <code>Reservation</code> class. What happens if you add a new property to <code>Reservation</code>? What happens if you have similar assertions scattered over the code base?
</p>
<p>
This is one reason that <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> also applies to unit tests. You want to have as few places as possible that you have to edit when you make changes. Otherwise, the risk increases that you forget one or more.
This is one reason that <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> also applies to unit tests. You want to have as few places as possible that you have to edit when making changes. Otherwise, the risk increases that you forget one or more.
</p>
<p>
Not only is the assertion brittle - it's also noisy, because it's hard to read. There's parsing, null coalescing and object initialisation going on in those four lines of Boolean operations. Perhaps it'd be better to extract a well-named helper method, but while I'm often in favour of doing that, I'm also a little concerned that too many ad-hoc helper methods obscure something essential. After all:
Expand Down

0 comments on commit e2539f4

Please sign in to comment.