diff --git a/_posts/2017-10-4-from-design-patterns-to-category-theory.html b/_posts/2017-10-4-from-design-patterns-to-category-theory.html index 25c743aae..ee6339bbb 100644 --- a/_posts/2017-10-4-from-design-patterns-to-category-theory.html +++ b/_posts/2017-10-4-from-design-patterns-to-category-theory.html @@ -213,7 +213,7 @@

diff --git a/_posts/2018-03-19-functors-applicatives-and-friends.html b/_posts/2018-03-19-functors-applicatives-and-friends.html index b2e381e6e..b02b517b9 100644 --- a/_posts/2018-03-19-functors-applicatives-and-friends.html +++ b/_posts/2018-03-19-functors-applicatives-and-friends.html @@ -76,7 +76,7 @@ diff --git a/_posts/2021-06-14-new-book-code-that-fits-in-your-head.html b/_posts/2021-06-14-new-book-code-that-fits-in-your-head.html index 1f769c157..bddf9bed8 100644 --- a/_posts/2021-06-14-new-book-code-that-fits-in-your-head.html +++ b/_posts/2021-06-14-new-book-code-that-fits-in-your-head.html @@ -66,6 +66,7 @@

  • Simplifying code with Decorated Commands
  • Unit testing private helper methods
  • Keep IDs internal with REST
  • +
  • The Equivalence contravariant functor
  • 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 restaurant reservation domain, it's from the book's code base.

    diff --git a/_posts/2021-09-02-contravariant-functors.html b/_posts/2021-09-02-contravariant-functors.html index 2dca2b3c7..5ac8afc11 100644 --- a/_posts/2021-09-02-contravariant-functors.html +++ b/_posts/2021-09-02-contravariant-functors.html @@ -78,7 +78,7 @@

    diff --git a/_posts/2021-09-09-the-specification-contravariant-functor.html b/_posts/2021-09-09-the-specification-contravariant-functor.html index d6b13e819..c1460e95f 100644 --- a/_posts/2021-09-09-the-specification-contravariant-functor.html +++ b/_posts/2021-09-09-the-specification-contravariant-functor.html @@ -237,6 +237,6 @@

    Are you noticing a pattern?

    - Next: Reader as a contravariant functor. + Next: The Equivalence contravariant functor.

    \ No newline at end of file diff --git a/_posts/2021-09-20-keep-ids-internal-with-rest.html b/_posts/2021-09-20-keep-ids-internal-with-rest.html index f22f8d61f..9459517c4 100644 --- a/_posts/2021-09-20-keep-ids-internal-with-rest.html +++ b/_posts/2021-09-20-keep-ids-internal-with-rest.html @@ -176,7 +176,7 @@

    This enables the LinksFilter and other internal code to still access the Id property, while the unit tests no longer could. As expected, this change caused some compiler errors. That was expected, and my plan was to lean on the compiler, as Michael Feathers describes in Working Effectively with Legacy Code.

    - 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 xUnit Test Patterns terms it. I'll return to that test in a future article. + 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 xUnit Test Patterns terms it. I'll return to that test in a future article.

    None of my self-hosted integration tests failed. diff --git a/_posts/2021-09-08-the-equivalence-contravariant-functor.html b/_posts/2021-09-27-the-equivalence-contravariant-functor.html similarity index 97% rename from _posts/2021-09-08-the-equivalence-contravariant-functor.html rename to _posts/2021-09-27-the-equivalence-contravariant-functor.html index 3dc65aa9b..6c188233e 100644 --- a/_posts/2021-09-08-the-equivalence-contravariant-functor.html +++ b/_posts/2021-09-27-the-equivalence-contravariant-functor.html @@ -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 %} @@ -12,10 +12,10 @@ {{ page.description }}

    - This article is an instalment in an article series about contravariant functors. It assumes that you've read the introduction. In previous articles, you saw examples of the Command Handler and Specification contravariant functors. This article presents another example. + This article is an instalment in an article series about contravariant functors. It assumes that you've read the introduction. In previous articles, you saw examples of the Command Handler and Specification contravariant functors. This article presents another example.

    - In a recent article I described how I experimented with removing the id 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 recent article I described how I experimented with removing the id 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.

    Baseline # @@ -79,7 +79,7 @@

    Brittle assertion #

    - When I made the Id property internal, this test no longer compiled. I had to delete the assignment of Id, which also meant that I couldn't use a deterministic Guid to define the expected value. While I could create an arbitrary Guid, that would never pass the test, since the Post method also generates a new Guid. + When I made the Id property internal, this test no longer compiled. I had to delete the assignment of Id, which also meant that I couldn't use a deterministic Guid to define the expected value. While I could create an arbitrary Guid, that would never pass the test, since the Post method also generates a new Guid.

    In order to get to green as quickly as possible, I rewrote the assertion: @@ -99,7 +99,7 @@

    It's brittle because it explicitly considers the four properties At, Email, Name, and Quantity of the Reservation class. What happens if you add a new property to Reservation? What happens if you have similar assertions scattered over the code base?

    - This is one reason that DRY 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 DRY 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.

    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: