<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jamietwells</title>
    <description>The latest articles on DEV Community by jamietwells (@jamietwells).</description>
    <link>https://dev.to/jamietwells</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F225251%2Fcd501114-6312-4d29-8962-cb27c316849e.jpeg</url>
      <title>DEV Community: jamietwells</title>
      <link>https://dev.to/jamietwells</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamietwells"/>
    <language>en</language>
    <item>
      <title>Enumerable types and interfaces, which type do you prefer to return?</title>
      <dc:creator>jamietwells</dc:creator>
      <pubDate>Tue, 19 Nov 2019 23:39:05 +0000</pubDate>
      <link>https://dev.to/jamietwells/enumerable-types-and-interfaces-4fo2</link>
      <guid>https://dev.to/jamietwells/enumerable-types-and-interfaces-4fo2</guid>
      <description>&lt;p&gt;A discussion I've been involved with recently has been an updated guideline around the return types of interfaces. It was decided that &lt;code&gt;IEnumerable&lt;/code&gt; was to not be used unless it was required that results be yielded rather than available immediately. There was to be a new rule that whenever you saw an &lt;code&gt;IEnumerable&lt;/code&gt; you change it to &lt;code&gt;ICollection&lt;/code&gt; to make it clear the results are immediately available.&lt;/p&gt;

&lt;p&gt;I disagree with this change for many reasons.&lt;/p&gt;

&lt;p&gt;First, on principle. It seems like a change to protect against an implementation where the developer doesn't realise an enumerable might be lazy and the generator might be in an invalid state when the consumer requests the results (perhaps it has an &lt;code&gt;IDisposable&lt;/code&gt; dependency). This has never seemed like a convincing argument to me. We shouldn't limit design choices so that developers don't have to learn the language, they should understand the language they're writing in.&lt;/p&gt;

&lt;p&gt;Also, it has the effect that developers tend to call &lt;code&gt;.ToList()&lt;/code&gt; or &lt;code&gt;.ToArray()&lt;/code&gt; at the end of every method simply to satisfy the &lt;code&gt;ICollection&lt;/code&gt;. The caller might only be interested in &lt;code&gt;.Any()&lt;/code&gt; or &lt;code&gt;.Take(some)&lt;/code&gt; or doing further filtering. The creation and allocation of the intermediate collection is then wasted time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ICollection&lt;/code&gt; has methods like &lt;code&gt;Add&lt;/code&gt; and &lt;code&gt;Remove&lt;/code&gt;, meaning what was previously a beautiful, simple, immutable return type is now a bloated mutable mess of an interface. Worse is if someone actually does start using the &lt;code&gt;.Add&lt;/code&gt; and &lt;code&gt;.Remove&lt;/code&gt; methods but the implementation behind the interface returns an array (which for some reason that I've never quite understood implements the &lt;code&gt;ICollection&lt;/code&gt; interface) the calling code will start having runtime exceptions. &lt;/p&gt;

&lt;p&gt;I wouldn't be so averse to using &lt;code&gt;IReadOnlyCollection&lt;/code&gt; but unfortunately this idea was met with strong resistance as that type wasn't something other devs were familiar with and &lt;code&gt;ICollection&lt;/code&gt; was already used in some places in the code, where as &lt;code&gt;IReadOnlyCollection&lt;/code&gt; was not. The other issue brought up with &lt;code&gt;IReadOnlyCollection&lt;/code&gt; was that it wasn't a sub-type of &lt;code&gt;ICollection&lt;/code&gt;, it was on the same level. &lt;/p&gt;

&lt;p&gt;I'd always assumed it was a subtype but apparently not, it was added much later and so would have been a breaking change to make &lt;code&gt;ICollection&lt;/code&gt; implement &lt;code&gt;IReadOnlyCollection&lt;/code&gt;. Regardless, it is sort of a subtype because all the members declared on &lt;code&gt;IReadOnlyCollection&lt;/code&gt; are declared on &lt;code&gt;ICollection&lt;/code&gt; and more so that argument again I didn't find convincing. I also don't really understand why it had to be a subtype of &lt;code&gt;ICollection&lt;/code&gt; to be used instead, surely the added immutability would be reason enough.&lt;/p&gt;

&lt;p&gt;I've always been of the opinion that these sorts of decisions should be made on a case by case basis. There isn't always a clear cut answer for the correct return type for a collection. I would usually stick to simpler interfaces (&lt;code&gt;IEnumerable&lt;/code&gt; or &lt;code&gt;IReadOnlyCollection&lt;/code&gt;) but I'd be hesitant to make a blanket statement around which to use in general. What do you think? How do you decide on return types? Perhaps you agree with my colleagues and can put their arguments to me so that I can be on the same page and feel more comfortable with the new guidelines.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
    <item>
      <title>Dynamic types in strongly typed languages - always wrong?</title>
      <dc:creator>jamietwells</dc:creator>
      <pubDate>Fri, 18 Oct 2019 20:45:10 +0000</pubDate>
      <link>https://dev.to/jamietwells/dynamic-types-in-strongly-typed-languages-always-wrong-5ecd</link>
      <guid>https://dev.to/jamietwells/dynamic-types-in-strongly-typed-languages-always-wrong-5ecd</guid>
      <description>&lt;p&gt;Recently I had a discussion with some colleagues about an endpoint I had created. I was asked by the product team to add the functionality to export some data in a specific format. We had endpoints that already did similar things and they all worked something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Post some data to an endpoint&lt;/li&gt;
&lt;li&gt;Write a row in a "file" table and remember the ID.&lt;/li&gt;
&lt;li&gt;Raise a message with the data to a queue to start generating the document&lt;/li&gt;
&lt;li&gt;Return the file ID to the front end so it can poll and API for the state of the file (and download when ready)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There were several of these endpoints already. The API didn't use the payload for anything, it simply put it on a queue but all these endpoints were the same code copied and pasted over and over. The only difference: the DTO classes for the payload they accepted.&lt;/p&gt;

&lt;p&gt;Seeing this I decided I had to do something! I made a new endpoint that would become the standard export endpoint going forward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RJjtUme5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/standards.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RJjtUme5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://imgs.xkcd.com/comics/standards.png" alt="Standards XKCD"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was using C# so forgive me if you're unfamiliar, I will try and make it easy enough to follow. Here is the endpoint I wrote:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Route("/export/{type}/{documentName}/{format}")]
public Guid ExportDocument([FromBody]JObject payload,
    string type, string documentName, string format)
{
    var fileId = _fileRepo.NewFile(documentName, format);
    var message = new 
    {
        DocumentName = documentName,
        Format = format,
        Payload = payload,
        FileId: fileId
    };

    var queue = GetQueueFromType(type);
    queue.PublishMessage(ToJson(message));

    return fileId;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;sup&gt;(Not the real code, but close enough to get the idea)&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;In that example, the &lt;code&gt;JObject&lt;/code&gt; is a C# class for representing some arbitrary JSON object, and that comes from the body of the HTTP request.&lt;/p&gt;

&lt;p&gt;My thinking was: the next time someone needed to export a file in a different format they could use this endpoint and the only thing they would need to do is add their queue URL to mapping file (a simple JSON file). They could subscribe their new worker to the queue and messages would start making their way there.&lt;/p&gt;

&lt;p&gt;I was fairly pleased with the design but it proved controversial when it came to making the pull request. The sticking point: the &lt;code&gt;JObject&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Colleagues said things like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is C# not JavaScript, we should be have strongly typed objects. JObject is a code smell.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What if someone posts a payload that's missing data the worker needs to process the message? You should validate it on the API.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The API should serve as the documentation for what is a valid request. How will people know what to post if there isn't a DTO?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why are you trying to avoid creating new endpoints? We're not charged per endpoint!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I argued and still maintain this is the correct design for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No code duplication. You could argue that duplication could be minimal for the solution with multiple endpoints if they all called back into a generic method but I still feel like the multiple endpoints themselves are a form of duplication; it's just more obviously duplicated when the endpoints are various versions of the same method copied and pasted, as was the case here.&lt;/li&gt;
&lt;li&gt;No DTOs required. Who wants yet more useless classes in an already large codebase? I'm not fond of DTOs in C# anyway - I dislike the fact that the seem to find themselves spreading out everywhere unless they are kept on a short leash and I particularly dislike that they have to have public get and set methods for every property.&lt;/li&gt;
&lt;li&gt;Reduced coupling. If the worker that's creating the documents changes and requires a new format for the payload the API doesn't need to change. The frontend needs to pass the new format and the worker needs to be able to handle the new format but the API will just pass along the payload as before.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I also just don't think a strongly typed endpoint has much of an advantage. It's not going to catch a type mismatch at compile time, it's a web API, the JavaScript can send anything. It just causes a runtime error which is exactly the same as the runtime error the worker would experience if the payload it received was incorrect. Perhaps it acts as a sort of documentation, but I feel like you could achieve similar documentation just by writing a comment above the endpoint saying: &lt;em&gt;Check the worker to find out what shape of payload is expected for each document type.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I managed to get the pull request in, but I feel like everyone else would still prefer if I got rid of it and went back to the old method. What do you think? Was it really a bad design? Are there disadvantages I've not mentioned? Would you have done something different? I feel like I just can't see the other side of the debate here so please help me understand in the comments.&lt;/p&gt;

</description>
      <category>strong</category>
      <category>weak</category>
      <category>codedesign</category>
    </item>
    <item>
      <title>I need to stop saying "Unit Tests"</title>
      <dc:creator>jamietwells</dc:creator>
      <pubDate>Wed, 02 Oct 2019 18:50:48 +0000</pubDate>
      <link>https://dev.to/jamietwells/i-need-to-stop-saying-unit-tests-4f0c</link>
      <guid>https://dev.to/jamietwells/i-need-to-stop-saying-unit-tests-4f0c</guid>
      <description>&lt;p&gt;In my head there are two types of automated tests worth (in 99% of cases) talking about and I've usually called them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit Tests&lt;/li&gt;
&lt;li&gt;Integration Tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Recently I've realised that using those names comes with so much baggage that I need a better name for them. When I'm talking about unit tests I mean any test that starts at some public entry point of the code, goes all the way through and ends at some boundary with an external system. This is either a mocked dependency or is the response of the function/method called initially. This response or call to an external system then has some expected properties asserted to be true. The unit test says nothing about how many functions/methods are called, how many classes might be involved - nothing. The only restriction is that you don't go into some external system such as a database, a Web API, a filesystem etc.&lt;/p&gt;

&lt;p&gt;Is this not what everyone else thinks a unit test is?&lt;/p&gt;

&lt;p&gt;An integration test, then, is a test to ensure your system can connect and integrate with the external systems we mocked out in the unit tests. We're not testing the external systems, only that we integrate successfully with them. If they're under your control you test them using unit tests inside that system.&lt;/p&gt;

&lt;p&gt;Maybe I have the wrong idea and what I describe aren't unit tests and integration tests. Maybe there's a different name I should be using? I ask because I recently discussed unit tests with several people and got responses similar to:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Oh, I don't bother with unit tests, they're a waste of time. I don't like testing one method/function/class at a time&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unit testing? No, I prefer to test in the way the user will interact with the system&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Both of these objections seem completely crazy to me. Why on Earth would I write a test that does one method at a time? And why would I test something in a way the user wouldn't use it?&lt;/p&gt;

&lt;p&gt;One person I was talking to was actually complaining about how, in C#, interfaces couldn't have static functions defined on them (put aside how that would actually work in practise; the only thing I can think is making the class generic for that interface type or something) and this was a problem due to a "rule" their company had that said &lt;em&gt;every class should be mocked out for a unit test except the class under test&lt;/em&gt;! This meant that any "Helper" class or "Calculation" class had to be mocked out, which meant it had to adhere to an interface, and therefore had to have instance methods even if it was more suited to being a static class. It also meant that internal classes (and private classes) were not able to be used, because you wouldn't be able to test them and would therefore have holes in your testing.&lt;/p&gt;

&lt;p&gt;This is I do not understand.&lt;/p&gt;

&lt;p&gt;Why do companies make rules like this? The tests shouldn't drive the architecture. If the class you're testing does everything in one big method or calls 50 other internal classes it &lt;em&gt;shouldn't change your tests&lt;/em&gt;! Test &lt;em&gt;that&lt;/em&gt; class. Whatever it's up to, test it; and test it in the way a user would use your system. If a user shouldn't use some class because it is for code only inside your library then make it internal and don't test it directly. Please, don't make everything public &lt;em&gt;just&lt;/em&gt; for the tests.&lt;/p&gt;

&lt;p&gt;So that's my rant. If people think I mean tests testing every method in the code individually when I say unit test then I need a better name for it. I'm open to suggestions. Spec test? BDD test? Behaviour test? All these seem to mean different things to different people.&lt;/p&gt;




&lt;p&gt;Is this the meaning you ascribe to "unit test"? Do you have a better name for what I describe? Does your company have an rules similar to the the rule I described above? Do you agree with the rule above? Let me know - I really want to know if I'm just missing something obvious or if we do need a better name for these things free of the baggage.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
