Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As ever, with design, you have contradictory requirements at play here. The art of good design is choosing the best balance between these requirements for the product you are creating.</p> <p>One principal of interface design is that you consider the interface to be a contract or promise. Its obvious that, the less prescriptive a promise or contract is, the easier it is to keep. So, a return type of <code>IEnumerable&lt;T&gt;</code> means you are promising to return only a sequence of things. A "thighter" or more derived type would be more prescriptive and, a harder promise to keep.</p> <p>Reflectively, if you are doing work in your function to ensure that a return type is more dervied, then it seems wasteful to abandon that work by returning the less perscriptive parent type. If you already have an <code>ISet&lt;T&gt;</code>, defining a set of things, be explicit, return that and save the caller the work of the distinct check.</p> <p>In general, there is no simple answer, the right answer depends on the caller and your system. We have no knowledge of the caller in the general case. In the absence of evidence, we must assume nothing and take the tangible benefit of the weaker promise. So, in general, go with <code>IEnumerable&lt;T&gt;</code> it gives you greater flexibility in the furture with no known cost now.</p> <p>In the specific case, where we know the caller will benefit from the return of a set. This is you case where a bug is raised on the distinctness of the sequence. It makes sense to be explicit and return an <code>ISet&lt;T&gt;</code>, this will help prevent duplication of the distinct check. To add a caveat, this only makes sense if the data will be distinct for every possible call. You must ensure you will keep the promise for all callers, not just those who have raised the bug.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload