Avoid Potential Problems with Explicit API Design
Tags: design
Published: 2019-04-06 10:46:34

---
title: Avoid Potential Problems with Explicit API Design
published: true
description: Avoid potential problems with explicit API design. The API contract should be clear what it offers
tags: Design
canonical_url: https://www.thaianhduc.com/2019/01/avoid-potential-problems-with-explicit-api-design/
---
<!-- wp:paragraph -->
<p>A year ago, I wrote Leaky <a aria-label="Abstraction - Linq Usage (opens in a new tab)" rel="noreferrer noopener" href="https://www.thaianhduc.com/2017/11/leaky-abstraction-linq-usage/" target="_blank">Abstraction - Linq Usage</a>. I use the design whenever I meet the same challenge. The system works as expected. I am happy about the design.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Until recently, there was a need to order the collection. Say that we need to order a TeacherCollection by years of experience. Assuming that there is a requirement to print the result in the years of experience order.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is a pretty simple requirement. In the TeacherCollection constructor, this code will do the job.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>public TeacherCollection(IEnumerable&lt;Teacher> teachers)
        {
            if (teachers != null)
                _teachers = teachers.Where(x => x.IsStillAtWork).OrderBy(x => x.StartedOn).ToList();
        }</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Everything works as expected.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Boom! The system is very slow when having more data. The profiler shows that a high number of time spent on the ordering. In that system, the ordering logic is much more complicated. It is not a pure Linq sorting as in the example. Still, the sorting cannot be a problem. That is for sure.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The problem is that the collection is accessed too many times. It is also an expected result because the collection is designed to filter data, to work in a pipeline in a safe way.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The ordering logic should not be placed here. The collection itself has all the information to do the sorting, filtering.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What should we change in<em> term of the design to solve the problem and also support the sorting</em>?</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>Identify Responsibilities</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In my opinion, this is the most difficult part of writing code. I have not found any exact formula to get it right. Identifying responsibilities is a heuristic. Experience matters here.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Filtering and ordering should be treated as two separated responsibilities. It is very easy to mix them in one implementation and thus error-prone. When defining responsibility, one should consider at least 2 factors</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The purpose of each: One is for filtering, the other is for sorting. They are 2 different operations.</li><li>When it is used and the usage frequency. Filtering is used a lot to extract sub collection from the original collection. Ordering is, on the other hand, only used when a final result is displayed to the end user or other form of presentation such as Console screen, word document.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>It is kind of tricky to see them as separated responsibilities. In many case I even not bother to think about it. Well, It proves that I was wrong. Sometimes, it sounds cool and simple if just order the list.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":5} -->
<h5>Extract Explicit Interfaces</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before moving on, let's take a look at the TeacherCollection. The additional feature we need is the ability to get exact index of a teacher.</p>
<!-- /wp:paragraph -->
{% gist https://gist.github.com/thaianhduc/b5a3a05d37affce4c540c8bc837b1fa0 %}

<!-- wp:paragraph -->
<p><strong>ITeacherCollection</strong> - The default interface is extracted from the current TeacherCollection. The School now holds an instance of ITeacherCollection, instead of TeacherCollection implementation. This refactoring step will not break anything.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>IIndexedTeacherCollection</strong> - A simple interface which supply only GetIndex API. A key point is that a consumer cannot instantiate it. The only way to have this API is a transition from ITeacherCollection.BuildIndex.</p>
<!-- /wp:paragraph -->

{% gist https://gist.github.com/thaianhduc/068a967163955e13c87fbcbb8e1fface %}

<!-- wp:paragraph -->
<p>The sorting cost is paid only whenever a need arises. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The actual implementation of the improved design is almost identical with the original version. All the major implementation logic is there in the TeacherCollection class. The refactoring is safe because the compiler will tell us what goes wrong.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The client (Program class) only deals with interfaces and interfaces transition.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Why didn't I have the ITeacherCollection at the first time? Well, we have not needed it at that time. We should not make thing complicated if there is no demand. Design is evolved.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I just solved a bug beautifully.</p>
<!-- /wp:paragraph -->